package grabber;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NoteboksGrabber {
    public static final String BASE_URL = "http://www.nbprice.ru/";

    public static void main(String[] args) throws IOException, WriteException {
        Document doc = Jsoup.connect(BASE_URL + "catalog/series-list/24.htm").get();
        Elements links = doc.select("div.break a");
        List<Notebook> notebooks = new ArrayList<Notebook>();
        for (Element linkElem : links) {
            String linkModel = linkElem.attr("href");
            Notebook notebook = new Notebook();
            notebook.models = new ArrayList<String>();
            List<String> processedModels = new ArrayList<String>();
            addModels(notebook, processedModels, BASE_URL + linkModel);
            for (int year = 2002; year < 2013; year++) {
                String noPriceUrl = BASE_URL + linkModel.replace(".htm", "_noprice_" + year + ".htm");
                addModels(notebook, processedModels, noPriceUrl);
            }
            Collections.sort(notebook.models);
            notebooks.add(notebook);
        }

        File xlsFile = new File("xls/notebooks.xls");
        WorkbookSettings s = new WorkbookSettings();
        WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, s);
        workbook.createSheet("sheet", 0);
        WritableSheet sheet = workbook.getSheet(0);
        int col = 0;
        for (Notebook notebook : notebooks) {
            System.out.println(notebook.brandName);
            sheet.addCell(new Label(col, 0, notebook.brandName));
            int row = 1;
            for (String model : notebook.models) {
                System.out.println("\t" + model);
                sheet.addCell(new Label(col, row++, model));
            }
            col++;
        }
        workbook.write();
        workbook.close();
    }

    private static void addModels(Notebook notebook, List<String> processedModels, String url) throws IOException {
        Document doc;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            return;
        }
        Elements models = doc.select("td[nowrap] a[href]");
        for (Element modelElem : models) {
            String[] modelFields = modelElem.text().split("\\s");
            String modelField = modelFields[0].trim();
            if (modelField.isEmpty()) {
                continue;
            }
            notebook.brandName = modelField;
            String model = "";
            List<String> processedFields = new ArrayList<String>();
            for (String field : modelFields) {
                field = field.trim();
                if (field.isEmpty()) {
                    continue;
                }
                if (field.contains("-")) {
                    field = field.substring(0, field.indexOf("-"));
                }
                if (field.equals(notebook.brandName)) {
                    continue;
                }
                if (processedFields.contains(field)) {
                    continue;
                }
                model += field + " ";
                processedFields.add(field);
            }
            model = model.trim();
            if (processedModels.contains(model)) {
                continue;
            }
            notebook.models.add(model);
            processedModels.add(model);
        }
    }

    static class Notebook {
        String brandName;
        List<String> models;
    }
}
